<template>
  <div class="person">
    <h2>汽车信息car：一辆{{ car.brand }}车，价值{{ car.price }}万</h2>
    <h2>汽车信息car1：一辆{{ car1.brand }}车，价值{{ car1.price }}万</h2>
    <button @click="changeBrand">修改汽车的品牌</button>
    <button @click="changePrice">修改汽车的价格</button>
    <button @click="changeCar">修改汽车</button>
    <hr>
    <h2>当前求和为：{{ sum }}</h2>
    <button @click="changeSum">点我sum+1</button>
    <h2>学生信息student：姓名{{ student.name }},性别{{ student.sex }},喜欢{{ student.xingqu.xName }}</h2>
    <button @click="changeStudent">修改兴趣</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import {ref, reactive, toRefs} from 'vue'

// 数据
let car = reactive({ brand: '奔驰', price: 100 })
let car1 = ref({ brand: '奔驰', price: 100 })
let sum = ref(0)

// 方法
function changeBrand() {
  car.brand = '宝马'
  car1.value.brand = '宝马'
}
function changePrice() {
  car.price += 10
  car1.value.price += 10

}
function changeCar() {
  // car = {brand:'奥拓',price:1} //这么写页面不更新的
  // car = reactive({brand:'奥拓',price:1}) //这么写页面不更新的

  // 下面这个写法页面可以更新(覆盖追加到原对象里面)
  Object.assign(car, { brand: '奥拓', price: 1 })
  car1.value={ brand: '奥拓', price: 1 }
}
function changeSum() {
  // sum = ref(9) //这么写页面不更新的
  sum.value += 1
}

/* toRefs 和 toRef*/

 let student=reactive({
   name:"张三",
   sex:"钕",
   xingqu:{
     xName:"睡觉"
   }
 })

let {name,sex,xingqu}=student
console.log(name,sex,xingqu)
function changeStudent(){
   name="李四" // 这是 并不是student 对象里面的name属性，而是name变量
   xingqu.xName="看电影"
 }


 console.log(toRefs(student)) // 里面的每一个key 都变成了 objectRefImpl,
 console.log(ref({
   name:"张三",
   sex:"钕",
   xingqu:{
     xName:"睡觉"
   }
 }))


</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}

button {
  margin: 0 5px;
}

li {
  font-size: 20px;
}
</style>
